﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;

namespace cnFederal
{
    public class CnfImages
    {
        private static CnfiDataDataContext lnq;
        private static List<CnfImage> _imagelist;

        public List<CnfImage> ImageList
        {
            get
            {
                return _imagelist;
            }
            set
            {
                _imagelist = value;
            }
        }

        public static void Load()
        {
            lnq = new CnfiDataDataContext();
            if (_imagelist == null)
            _imagelist = new List<CnfImage>();
            if (_imagelist != null)
            {
                List<CnfImage> liste = new List<CnfImage>();
                var b = from c in lnq.Img_Tables
                        select c;

                foreach (var s in b)
                {
                    CnfImage img = new CnfImage();
                    img.Id = (int)s.Id;
                    img.Image = (Binary)s.Image;
                    img.ImageName = (string)s.ImageName;
                    img.LinkId = (int)s.LinkId;
                    img.LinkType = (int)s.LinkType;

                    liste.Add(img);
                }

                _imagelist = liste;
            }
        }

        public void RefreshPostlist()
        {
            _imagelist.Clear();
            _imagelist = GetAllImages();
        }

        public static List<CnfImage> GetAllImages()
        {

            return Enumerable.Reverse(_imagelist).ToList(); ;
        }

        public static CnfImage GetLastAddedImage()
        {
            var b = from c in lnq.Img_Tables
                    orderby c.Id descending
                    select c;
            Img_Table img_T = b.First();
            CnfImage img = new CnfImage();
            img.Id = img_T.Id;
            img.Image = img_T.Image;
            img.ImageName = img_T.ImageName;
            img.LinkId = img_T.LinkId;
            img.LinkType = img_T.LinkType;

            return img;
        }
        public static CnfImage GetImageById(int linkId, int linkTypeId)
        {
            if (_imagelist == null) return null;
            CnfImage img = null;
            var b = from c in _imagelist
                    where c.LinkId == linkId && c.LinkType == linkTypeId
                    select c;
            foreach (var s in b)
            {
                img = new CnfImage();
                img.Id = (int)s.Id;
                img.Image = (Binary)s.Image;
                img.ImageName = (string)s.ImageName;
                img.LinkId = (int)s.LinkId;
                img.LinkType = (int)s.LinkType;
            }

            return img;
        }

        public static CnfImage GetImageByName(string name)
        {
            if (_imagelist == null) return null;
            CnfImage img = null;
            var b = from c in _imagelist
                    where c.ImageName == name
                    select c;
            foreach (var s in b)
            {
                img = new CnfImage();
                img.Id = (int)s.Id;
                img.Image = (Binary)s.Image;
                img.ImageName = (string)s.ImageName;
                img.LinkId = (int)s.LinkId;
                img.LinkType = (int)s.LinkType;
            }

            return img;
        }

        public static CnfImage GetImageById(int id)
        {
            if (_imagelist == null) return null;
            CnfImage img = null;
            var b = from c in _imagelist
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {
                img = new CnfImage();
                img.Id = (int)s.Id;
                img.Image = (Binary)s.Image;
                img.ImageName = (string)s.ImageName;
                img.LinkId = (int)s.LinkId;
                img.LinkType = (int)s.LinkType;
            }

            return img;
        }

        public static bool CreateNew(CnfImage cnfimage)
        {
            bool created = false;
            try
            {
                Img_Table img_t = new Img_Table();
                img_t.Image = cnfimage.Image;
                img_t.ImageName = cnfimage.ImageName;
                img_t.LinkId = cnfimage.LinkId;
                img_t.LinkType = cnfimage.LinkType;
                lnq.Img_Tables.InsertOnSubmit(img_t);
                lnq.SubmitChanges();
                lnq.Connection.Close();
                created = true;
            }
            catch (Exception ex)
            {
                created = false;
            }
            return created;
        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.Img_Tables
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                Img_Table n = b.First();
                try
                {
                    lnq.Img_Tables.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;
        }


        public static void Save(CnfImage fv)
        {
            var b = from f in lnq.Img_Tables
                    where f.Id == fv.Id
                    select f;
            foreach (var n in b)
            {

                n.Image = fv.Image;
                n.ImageName = fv.ImageName;
                n.LinkType = fv.LinkType;
                n.LinkId = fv.LinkId;

            }
            lnq.SubmitChanges();
        }











    }

    public class CnfImage
    {
        public int Id
        {
            get;
            set;
        }

        public System.Data.Linq.Binary Image
        {
            get;
            set;
        }

        public string ImageName
        {
            get;
            set;
        }

        public int LinkType
        {
            get;
            set;
        }

        public int LinkId
        {
            get;
            set;
        }
    }
}